--Merchant generation script (default settings create 20-30 merchant ships in the China Sea assigned to a ferry mission named 'VLADIVOSTOK' on a side named 'Merchant'
math.randomseed(os.time())
merch_num = math.random(20,30) --change 20 or 30 to your specified minimum and maximum number of merchants
for i = 1,merch_num do
redo_count = 0
::redo::
local lat_var = math.random(1,(10^13))
local lon_var = math.random(1,(10^13))
v_lat = math.random(34,40) + (lat_var/(10^13)) --change 34 and 40 to your specified minimum and maximum latitude values; it's important that the first number is smaller than the second.
v_lon = math.random(119,126) + (lon_var/(10^13)) --change 119 and 126 to your specified minimum and maximum longitude values; it's important that the first number is smaller than the second.
elevation = World_GetElevation({latitude=v_lat, longitude=v_lon})
if elevation > -10 then --Checks to see if the water is deep enough, adjust as you please
redo_count = redo_count + 1
if redo_count > 100 then
--print ('A ship was not able to find a suitable spot for placement. Re-check latitude and longitude settings')
goto redo --retries the placement if the water is too shallow
else
goto redo --retries the placement if the water is too shallow
end
end
DBIDTABLE = { 775, 2027, 2029, 2028, 2030, 774, 2026, 2031, 2775, 2023, 773, 2774, 1001, 1374, 2773, 2776, 1006, 222, 1599, 2034, 1002, 1317, 144, 339, 275, 145, 2022, 259 } --list of DBIDs
DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)]
local new_merch = ScenEdit_AddUnit({side='Merchant', type='Ship',name='Merchant #'..i, dbid=DBID,latitude=v_lat,longitude=v_lon})
local fuel = new_merch.fuel
fuel[3001].current = fuel[3001].max*math.random(600, 800)/1000
new_merch.fuel = fuel

----------------------------------------------------
--list and choise of course:
----------------------------------------------------
Courses = { 
-- Course n1 (Port A-Port B-Port C):
{ 
[1] = {TypeOf = 'ManualPlottedCourseWaypoint',latitude=37.2681434320673, longitude=122.59455932607}, -- Direction Port A
[2] = {TypeOf = 'ManualPlottedCourseWaypoint', latitude=31.2704422605587, longitude=122.014721160601}, -- Direction Port B
[3] = {TypeOf = 'ManualPlottedCourseWaypoint', latitude=35.5104815834653, longitude=126.293384175094} -- Direction Port C
}, 
-- Course n2 (Port D-Port E-Port F):
{ 
[1] = {TypeOf = 'ManualPlottedCourseWaypoint', latitude=35.5486485738367, longitude=132.88663277135}, -- Direction Port D
[2] = {TypeOf = 'ManualPlottedCourseWaypoint', latitude=37.4361334445333, longitude=136.808678667868}, -- Direction Port E
[3] = {TypeOf = 'ManualPlottedCourseWaypoint', latitude=42.8284933260854, longitude=132.556812432586} -- Direction Port F
}, 
-- Course n3 (Port C-Port A-Port F):
{ 
[1] = {TypeOf = 'ManualPlottedCourseWaypoint', latitude=35.5104815834653, longitude=126.293384175094}, --  Direction Port C
[2] = {TypeOf = 'ManualPlottedCourseWaypoint',latitude=37.2681434320673, longitude=122.59455932607}, -- Direction Port A
[3] = {TypeOf = 'ManualPlottedCourseWaypoint', latitude=42.8284933260854, longitude=132.556812432586} -- Direction Port F
} 
} 
randomCourse = Courses[math.random(1,#Courses)] --Random choice of the course

----------------------------------------------------
--list and choise of port:
----------------------------------------------------
PortTable ={"Port A","Port B","Port C","Port D","Port E","Port F"} --list of Port
Port= PortTable[math.random( 1, #PortTable)] --Random choice of the port
--------------------------------------------------


ScenEdit_SetUnit({side='Merchant',unitname=new_merch.name,base=Port,course = randomCourse}) --use to assign to a base and a course 


--ScenEdit_SetUnit({side='Merchant',unitname=new_merch.name,base=Port,RTB=true}) --use to assign to a base and order a RTB 
--ScenEdit_AssignUnitToMission( new_merch.name, 'VLADIVOSTOK') --use to assign to a mission
print (new_merch.name..' with dbid '..DBID..' was created in water with a depth of '..elevation..'m')
end